package com.zpc.myvideoserver.common.util;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName : RedisUtil  //类名
 * @Description : Redis 工具类  //描述
 * @Author : zpc20 //作者
 * @Date: 2023/11/5  19:34
 */
@Service
@Data
@NoArgsConstructor
public class RedisUtil {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    public void setBoundValueOperations(String key) {
        this.boundValueOperations = redisTemplate.boundValueOps(key);
    }

    public void setBoundHashOperations(String key) {
        this.boundHashOperations = redisTemplate.boundHashOps(key);
    }

    private BoundValueOperations<String,Object> boundValueOperations;

    private BoundHashOperations<String,Object,Object> boundHashOperations;

    public RedisUtil(BoundValueOperations<String, Object> boundValueOperations,BoundHashOperations<String,Object,Object> boundHashOperations) {
        this.boundValueOperations = boundValueOperations;
        this.boundHashOperations = boundHashOperations;
    }
    /**
     * @Name: expire
     * @Params : [key, time]:[java.lang.String, long]
     * @Description : 根据key增加过期时间
     * @Author : zpc20
     * @Date : 2023/11/5 19:37
     * @Return boolean
     * @Throws
     */
    public void expire(String key, long time){
        redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }
    /**
     * @Name: expire
     * @Params : [time]:[long]
     * @Description : 绑定值增加过期时间
     * @Author : zpc20
     * @Date : 2023/11/5 22:40
     * @Return void
     * @Throws
     */
    public void expire(long time){

        boundValueOperations.expire(time, TimeUnit.SECONDS);
    }
    /**
     * @Name: set
     * @Params : [key, value]:[java.lang.String, java.lang.Object]
     * @Description : 缓存赋值
     * @Author : zpc20
     * @Date : 2023/11/5 21:59
     * @Return boolean
     * @Throws
     */
    public boolean  set(String key,Object value){

        try {
           redisTemplate.opsForValue().setIfAbsent(key,value);
           return true;
       }catch (Exception e){
           e.printStackTrace();
       }
       return false;
    }
    public boolean set(Object value){
        try {
            boundValueOperations.set(value);
            return  true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public void setByExpire(String key, Object value, long time){
        try {
            redisTemplate.opsForValue().setIfAbsent(key,value,time,TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean hsetByExpire(Map<String,Object> value, long time){
        try {
            // redisTemplate.opsForHash().put("test1","name","zpc");
             boundHashOperations.putAll(value);
             boundHashOperations.expire(time,TimeUnit.SECONDS);
             return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    public List<Object> HashMultiget(Collection<Object> value){
        try {
             return boundHashOperations.multiGet(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean setByExpire(Object value,long time){
        try {
            boundValueOperations.setIfAbsent(value,time,TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
